package com.petalsgroup.petals.dao.user.impl;

import com.petalsgroup.petals.dao.user.CommentsDao;
import com.petalsgroup.petals.pojo.CommentsDO;
import com.petalsgroup.petals.util.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CommentsDaoImpl implements CommentsDao {
    //使用数据库连接池
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<CommentsDO> findCommentsByUid(Integer uid) {
        String sql = "select * from comments where uid = ?";
        List<CommentsDO> list = template.query(sql, new BeanPropertyRowMapper<>(CommentsDO.class), uid);
        return list;
    }

    @Override
    public void deleteCommentsByid(Integer comid) {
        String sql = "delete from comments where com_id = ?";
        template.update(sql,comid);
    }

    @Override
    public CommentsDO findCommentsByAid(Integer aid) {
        String sql = "select * from comments where aid = ?";
        List<CommentsDO> list = template.query(sql, new BeanPropertyRowMapper<>(CommentsDO.class), aid);
        //System.out.println(list);
        return list.get(0) ;
    }

    @Override
    public Integer findAidByCid(Integer comid) {
        String sql = "select * from comments where com_id = ?";
        List<CommentsDO> list = template.query(sql, new BeanPropertyRowMapper<>(CommentsDO.class), comid);

        return list.get(0).getAid();
    }

    @Override
    public void updateNum(Integer aid) {
        String sql="update animation_simple set people_num=people_num-1 where aid=?;";
        try {
            template.update(sql,aid);
        }catch (Exception e){}
    }


}
